<?php
include ('_includes/lock.php');
include ('_class/Navigator.php');
include ('_class/DataBaseConnector.php');
$id = NULL;
if (isset($_GET['id'])) {
    $id = $_GET['id'];
} else {
    header("location: index.php");
}

function showContent() {
    $id = $_GET['id'];
    include_once('_class/DataBaseConnector.php');
    $db = new DataBaseConnector();
    $db->connect();
    $selectedData = $db->selectDataWhere("articles", "title,bodytext,date,isDoc", "id=$id");
    $res = "Not Found!";
    if ($selectedData != NULL) {
        $row = mysql_fetch_assoc($selectedData);
        $content = $row['bodytext'];
        $title = $row['title'];
        $date = $row['date'];
        $isFile = $row['isDoc'];

        if ($isFile) {
            $res = "<h1>$title</h1>
                <p>Date of creation: $date</p>
                <a href=\"PDF\\$title\" class=\"large blue link\">Download</a>";
        } else {
            $allLinks = "";
            $links = $db->selectDataWhere("articles", "title,id", "art_id=$id");
            if ($links != NULL) {
                while ($row2 = mysql_fetch_assoc($links)) {
                    $titleLink = $row2['title'];
                    $idLink = $row2['id'];
                    $allLinks.= " <a href=\"entry.php?id=$idLink\">$titleLink</a> ";
                }
            } else {
                $allLinks = "No child documents.";
            }

            $res = "<h1>$title</h1>
                <p>Date of creation: $date</p>
                <p>$content</p>
                <h3>Child Documents</h3>
                <p>$allLinks</p>
                <h3>Options</h3>";

            if ($_SESSION['user_role'] == "ADMIN") {
                $res .= "<a href=\"edit_article.php?id=$id\">Edit entry</a> |
                     <a href=\"new_article.php?id=$id\">Add child document</a> |
                     <a href=\"new_file.php?id=$id\">Upload a document</a>";
            }
        }
    }
    return $res;
}

function addNavElement() {
    $pageName = $_SERVER['PHP_SELF'];
    if (isset($_GET['id'])) {
        $id = $_GET['id'];
        $db = new DataBaseConnector();
        $db->connect();
        $selectedData = $db->selectDataWhere("articles", "title,isroot", "id=$id");
        if ($selectedData != NULL) {
            $row = mysql_fetch_assoc($selectedData);
            $title = $row['title'];
            $isRoot = $row['isroot'];

            if ($isRoot) {
                $navigator = new Navigator();
                $_SESSION['nav'] = serialize($navigator);
            }

            $element = new NavElement($title, $pageName, $id);
            $navigator = unserialize($_SESSION['nav']);
            $stacks = $navigator->stack;
            $searchRes = array_search($element, $stacks);
            if ($searchRes == NULL) {
                array_push($stacks, $element);
                $navigator->stack = $stacks;
                $_SESSION['nav'] = serialize($navigator);
            } else {
                $found = false;
                while (!$found) {
                    $last = array_pop($stacks);

                    if ($element->id == $last->id) {
                        array_push($stacks, $element);
                        $navigator->stack = $stacks;
                        $_SESSION['nav'] = serialize($navigator);
                        $found = true;
                    }
                }
            }
        }
    }
}
?>
<html lang="en">
    <head>
        <title>Computer Science</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" href="resources/styles.css" type="text/css" media="screen" />
    </head>
    <body>
        <?php ?>
        <div id="wrapper">
            <header></header>
            <?php addNavElement() ?>
            <?php include ('_includes/nav.php') ?>
            <div id="page">
                <div id="content">
                    <?php echo showContent() ?>
                </div>
                <?php include ('_includes/sidebar.php') ?>
            </div>
            <footer>
                <section>
                    <p>Copyright ©2012 - All Rights Reserved</p>
                </section>
            </footer>
        </div>
    </body>
</html>
